import { routerRedux } from "dva/router";
import {
  getModelDetail,
  saveModel,
  testConnection,
  getCandidateGroups,
  getDeploymentDetail,
  getNoticeChannelType,
  getTemplateListByType,
  getTemplateParameter,
  getUserGroupList,
  getAllPositionList,
  getUserRelationShipList,
  getAllFunctionConfigList,
  checkFlowChartUrl,
  testEvent,
  supplySignTask,
  deleteFile,
  getUserGroupPageList,
  getUserPositionPageList,
  getUserRolePageList,
  saveFunctionConfig,
  deleteFunctionConfig,
} from "../services/flowChartApi";
import {deleteDetailData} from "@/pages/flowmanage/entrust/server/EntrustApi";

const initData = {
  detailData: {},
  candidateGroupsData: [],
  noticeTypeData: [],
  flowNodesData: [], // 流程图节点

};

export default {
  namespace: "FlowChart",
  state: {
    ...initData,
  },

  effects: {
    *getModelDetail({ payload }, { call, put }) {
      const { errCode, errMsg, datas = {} } = yield call(
        getModelDetail,
        payload,
      );
      yield put({ type: "get/detailData", payload: datas || {} });
      return { errCode, errMsg };
    },
    *getDeploymentDetail({ payload }, { call, put }) {
      const { errCode, errMsg, datas } = yield call(
        getDeploymentDetail,
        payload,
      );
      if (errCode === 0) {
        yield put({ type: "get/detailData", payload: datas || {} });
      }
      return { errCode, errMsg };
    },
    *saveModel({ payload }, { call, put }) {
      const { errCode, errMsg, errortext } = yield call(saveModel, payload);
      if (errCode === 0) {
        yield put(
          routerRedux.push({
            pathname: "/flowmanage/flowconfigure",
          }),
        );
      }
      return { errCode, errMsg: errMsg || errortext };
    },
    *testConnection({ payload }, { call }) {
      const { errCode, errMsg, errortext, datas } = yield call(testConnection, payload);
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *getCandidateGroups({ payload }, { call, put }) {
      const { errCode, errMsg, datas } = yield call(
        getCandidateGroups,
        payload,
      );
      if (errCode === 0) {
        yield put({ type: "get/candidateGroupsData", payload: datas || [] });
      }
      return { errCode, errMsg, datas };
    },



    // 角色分页查询
    *getUserRolePageList({ payload }, { call }) {
      const res = yield call(
        getUserRolePageList,
        payload,
      );

      return res;
    },


    // 组织分页查询
    *getUserGroupPageList({ payload }, { call }) {
      const res = yield call(
        getUserGroupPageList,
        payload,
      );

      return res;
    },

    // 岗位分页查询
    *getUserPositionPageList({ payload }, { call }) {
      const res = yield call(
        getUserPositionPageList,
        payload,
      );

      return res;
    },


    *getNoticeChannelType(action, { call, select, put }) {
      const noticeTypeData = yield select(
        ({ FlowChart }) => FlowChart.noticeTypeData,
      );
      if (noticeTypeData && noticeTypeData.length > 0) {
        return noticeTypeData;
      }
      const { errCode, datas } = yield call(getNoticeChannelType);
      if (`${errCode}` === "0") {
        yield put({
          type: "set/noticeTypeData",
          payload: datas,
        });
        return datas;
      } else {
        yield put({
          type: "set/noticeTypeData",
          payload: [],
        });
        return [];
      }
    },
    *getTemplateListByType({ payload }, { call }) {
      const { errCode, errortext, datas } = yield call(
        getTemplateListByType,
        payload,
      );
      return { errCode, errortext, datas };
    },
    *getTemplateParameter({ payload }, { call }) {
      const { errCode, datas } = yield call(getTemplateParameter, payload);
      return { errCode, datas };
    },
    *getUserGroupList({ payload }, { call, put }) {
      const { errCode, errMsg, datas } = yield call(
        getUserGroupList,
        payload,
      );
      if (errCode === 0) {
        yield put({ type: "get/userGroupListData", payload: datas || [] });
      }
      return { errCode, errMsg, datas };
    },
    *getAllPositionList({ payload }, { call, put }) {
      const { errCode, errMsg, datas } = yield call(
        getAllPositionList,
        payload,
      );
      if (errCode === 0) {
        yield put({ type: "get/allPositionListData", payload: datas || [] });
      }
      return { errCode, errMsg, datas };
    },
    *getUserRelationShipList({ payload }, { call, put }) {
      const { errCode, errMsg, datas } = yield call(
        getUserRelationShipList,
        payload,
      );
      if (errCode === 0) {
        yield put({ type: "get/userRelationShipListData", payload: datas || [] });
      }
      return { errCode, errMsg, datas };
    },
    *getAllFunctionConfigList({ payload }, { call }) {
      const { errCode, errMsg, errortext, datas } = yield call(getAllFunctionConfigList, payload);
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *checkFlowChartUrl({ payload }, { call }) {
      const response = yield call(
        checkFlowChartUrl,
        payload,
      );
      // yield put({ type: "check/flowChartUrl", payload: datas || {} });
      return response;
    },
        *testEvent({ payload }, { call }) {
      const { datas = {}, errCode, errMsg, errortext } = yield call(
        testEvent,
        payload
      );
      return { errCode, errMsg: errMsg || errortext, datas };
    },

    *supplySignTask({ payload }, { call }) {
      const response = yield call(
        supplySignTask,
        payload,
      );
      return response;
    },

    *deleteFile({ payload }, { call }) {
      const { errCode, errMsg, errortext, datas } = yield call(deleteFile, payload);
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *saveFunctionConfig({ payload }, { call }) {
      const { errCode, errMsg, errortext, datas } = yield call(
        saveFunctionConfig,
        payload,
      );
      return { errCode, errMsg: errMsg || errortext, datas };
    },
    *deleteFunctionConfig({ payload }, { call }) {
      const res = yield call(deleteFunctionConfig, payload);
      return res;
    },
  },
  reducers: {

    "get/detailData": (state, { payload }) => ({
      ...state,
      detailData: payload,
    }),
    "get/candidateGroupsData": (state, { payload }) => ({
      ...state,
      candidateGroupsData: payload,
    }),
    "set/noticeTypeData": (state, { payload }) => ({
      ...state,
      noticeTypeData: payload,
    }),
    "set/flowNodesData": (state, { payload }) => ({
      ...state,
      flowNodesData: payload,
    }),
    "get/userGroupListData": (state, { payload }) => ({
      ...state,
      userGroupListData: payload,
    }),
    "get/allPositionListData": (state, { payload }) => ({
      ...state,
      allPositionListData: payload,
    }),
    "get/userRelationShipListData": (state, { payload }) => ({
      ...state,
      userRelationShipListData: payload,
    }),
    // "check/flowChartUrl":(state, { payload }) => ({
    //   ...state,
    //   flowChartUrl: payload,
    // }),
    clearData: () => ({ ...initData }),
  },
};
